package edu.sdwu.liqing.qasystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import edu.sdwu.liqing.qasystem.entity.QaModule;
import edu.sdwu.liqing.qasystem.entity.QaRole;
import edu.sdwu.liqing.qasystem.entity.QaRoleModule;
import edu.sdwu.liqing.qasystem.entity.QaUser;
import edu.sdwu.liqing.qasystem.mapper.QaModuleMapper;
import edu.sdwu.liqing.qasystem.service.IQaModuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.sdwu.liqing.qasystem.service.IQaQuestionService;
import edu.sdwu.liqing.qasystem.service.IQaRoleModuleService;
import edu.sdwu.liqing.qasystem.util.OperateUtil;
import edu.sdwu.liqing.qasystem.util.RequestUtil;
import edu.sdwu.liqing.qasystem.vo.QaModuleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author liqing@sdwu.edu
 * @since 2020-03-22
 */
@Service
public class QaModuleServiceImpl extends ServiceImpl<QaModuleMapper, QaModule> implements IQaModuleService {


    @Autowired
    RequestUtil requestUtil;

    @Autowired
    IQaRoleModuleService roleModuleService;

    @Autowired
    OperateUtil operateUtil;

    @Autowired
    IQaQuestionService questionService;

    @Override
    public boolean save(QaModule module) {
        QaUser user = requestUtil.getCurrentUser();
        if (StringUtils.isAnyBlank(module.getModuleName(), module.getModuleContent())) {
            return false;
        }
        boolean flag = super.save(module);
        if (flag) {
            List<QaRoleModule> moduleList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(module.getRoleModuleList())) {
                module.getRoleModuleList().parallelStream().forEach(i-> {
                    if (!StringUtils.equals(user.getRoleId(), i)) {
                        QaRoleModule qaRoleModule = new QaRoleModule();
                        qaRoleModule.setModuleId(module.getId());
                        qaRoleModule.setRoleId(i);
                        moduleList.add(qaRoleModule);
                    }
                });
            }
            //最后加入自己的权限
            QaRoleModule qaRoleModule = new QaRoleModule();
            qaRoleModule.setModuleId(module.getId());
            qaRoleModule.setRoleId(user.getRoleId());
            moduleList.add(qaRoleModule);
            roleModuleService.saveBatch(moduleList);
        }
        return flag;
    }

    @Override
    public boolean update(QaModule module) {
        QaModule qaModule = super.getById(module.getId());
        if (Objects.isNull(qaModule)) {
            return false;
        }
        if (!operateUtil.canOperate(module)) {
            return false;
        }
        return super.updateById(module);
    }

    @Override
    public boolean delete(String id) {
        if (StringUtils.isBlank(id)) {
            return false;
        }
        QaModule qaModule = super.getById(id);
        if (Objects.nonNull(questionService.getByModuleId(id))) {
            throw new RuntimeException("无法删除，当前模块下存在问题");
        }
        if (Objects.isNull(qaModule)) {
            return false;
        }
        if (!operateUtil.canOperate(qaModule)) {
            return false;
        }
        return super.removeById(id);
    }

    @Override
    public Page<QaModule> page(QaModuleVo vo) {
        QaUser user = requestUtil.getCurrentUser();
        QueryWrapper<QaModule> qw = Wrappers.query();
        qw.eq(StringUtils.isNotBlank(vo.getModuleName()), "module_name", vo.getModuleName());
        if (StringUtils.isNotBlank(vo.getKeyword())) {
            qw.nested(i -> {
               i.like("module_name", vo.getKeyword())
               .like("module_content", vo.getKeyword());
            });
        }
        //非admin要过滤
        if (!StringUtils.containsIgnoreCase("ADMIN", user.getRoleName())) {
            //需要查看过滤
            List<String> moduleList = roleModuleService.getModuleIdListByRoleId(user.getRoleId());
            if (CollectionUtils.isEmpty(moduleList)) {
                return vo.getPage();
            }
            qw.in("id", moduleList);
        }
        return super.page(vo.getPage(), qw);
    }

    /**
     * 这个方法没有参数 具体就是为了获取当前用户能查看的所有模块列表
     * */
    @Override
    public List<QaModule> list() {
        List<String> moduleList =  roleModuleService.getModuleIdListByRoleId(requestUtil.getCurrentUser().getRoleId());
        if (CollectionUtils.isEmpty(moduleList)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        QueryWrapper<QaModule> qw = Wrappers.query();
        qw.in("id", moduleList);
        List<QaModule> resultList = super.list(qw);
        return Objects.nonNull(resultList) ? resultList: Lists.newArrayListWithExpectedSize(0);
    }
}
